16 memset(number
,0,sizeof(number
));
25 // takes 'r' as right number and writes result to 'this'
27 HugeInt
* operator= (const HugeInt
* r
) {
28 memset(number
,0,sizeof(number
));
29 strcpy(number
,r
->number
);
35 HugeInt
* operator= (const int r
) {
36 memset(number
,0,sizeof(number
));
37 sprintf(number
,"%d",r
);
38 length
= strlen(number
);
39 for (int i
= 0; i
< (length
>> 1); i
++) {
41 number
[i
] = number
[length
-i
-1];
42 number
[length
-i
-1] = c
;
44 for (int j
= 0; j
< length
; j
++)
50 const HugeInt
operator+ (const HugeInt
& r
) {
51 int n
= max(length
,r
.length
), carry
= 0, k
, i
;
53 for (i
= 0; i
< n
|| carry
; i
++) {
54 k
= number
[i
] + r
.number
[i
] + carry
;
55 theNew
.number
[i
] = k
% 10;
63 void operator+= (const HugeInt
& r
) {
67 // *slightly veryfied*
68 const HugeInt
operator* (const int r
) {
71 for (i
= 0; i
< length
|| carry
; i
++) {
72 k
= number
[i
] * r
+ carry
;
73 theNew
.number
[i
] = k
% 10;
80 // *slightly veryfied*
81 void operator*= (const int r
) {
86 // shifts number left by 'shift' positions
87 // useful when multiplying two HugeInt's
88 HugeInt
operator<< (const int shift
) {
91 // don't shift if number is 0 and there is no number
92 //if (length == 0 || length == 1 && number[0] == 0) return;
93 for (i
= length
- 1; i
>= 0; i
--)
94 theNew
.number
[i
+ shift
] = number
[i
];
95 for (i
= 0; i
< shift
; i
++)
97 theNew
.length
= length
+ shift
;
101 HugeInt
operator* (HugeInt
& r
) {
104 for (i
= 0; i
< length
; i
++)
105 theNew
+= (r
<< i
) * number
[i
];
110 HugeInt
operator*= (HugeInt
& r
) {
115 int operator % (int r
) {
117 for (int i
= length
- 1; i
>= 0; i
--)
118 n
= (n
* 10 + number
[i
]) % r
;
122 int operator %= (int r
) {
126 HugeInt
operator/ (int r
) {
130 for (int i
= length
- 1; i
>= 0 || n
>= r
; i
--) {
131 n
= (n
* 10 + number
[i
]);
132 I
.number
[j
++] = n
/ r
;
137 for (int i
= 0; i
< j
/ 2; i
++)
138 swap(I
.number
[i
],I
.number
[j
-i
-1]);
145 HugeInt
operator /= (int r
) {
152 for (int i
= length
- 1; i
>= 0; i
--)
153 putchar(number
[i
] + '0');
156 void truncate(HugeInt
& n
) {
157 for (int i
= n
.length
- 1; i
>= 0 && n
.number
[i
] == 0; i
--)
165 HugeInt
power(int p
) {
167 for (int i
= 0; i
< p
; i
++)
172 bool operator<(const HugeInt
& rhs
) {
173 if (this->length
!= rhs
.length
)
174 return this->length
< rhs
.length
;
175 for (int i
= 0; i
< this->length
; i
++)
176 if (this->number
[i
] != rhs
.number
[i
])
177 return this->number
[i
] < rhs
.number
[i
];
182 if (length
== 1 && number
[0] == 0) {
193 while (cin
>> n
>> d
&& (n
+ d
)){
194 if (n
== 1 || d
== 0){